﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class PrinterQueueRsPrinterObject
    {
        public static PrinterQueueRsPrinter[] GetPrinterQueueRsPrinters()
        {
            return Query(null);
        }

        public static PrinterQueueRsPrinter GetPrinterQueueRsPrinter(int id)
        {
            PrinterQueueRsPrinter printerQueueRsPrinter = new PrinterQueueRsPrinter();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterQueueRsPrinterDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterQueueRsPrinterDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable printerQueueRsPrinters = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            printerQueueRsPrinter.Id                   = Convert.ToInt32(printerQueueRsPrinters.Rows[0][PrinterQueueRsPrinterDesc.Id]);
            printerQueueRsPrinter.PrinterId            = Convert.ToInt32(printerQueueRsPrinters.Rows[0][PrinterQueueRsPrinterDesc.PrinterId]);
            printerQueueRsPrinter.PrinterQueueId       = Convert.ToInt32(printerQueueRsPrinters.Rows[0][PrinterQueueRsPrinterDesc.PrinterQueueId]);
            printerQueueRsPrinter.Category             = Convert.ToInt32(printerQueueRsPrinters.Rows[0][PrinterQueueRsPrinterDesc.Category]);


            return printerQueueRsPrinter;
        }

        public static PrinterQueueRsPrinter[] Query(string condition)
        {

            // query factories from database
            string sql = "select * from " + PrinterQueueRsPrinterDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where " + condition;
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                PrinterQueueRsPrinter[] printerQueueRsPrinters = new PrinterQueueRsPrinter[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    PrinterQueueRsPrinter printerQueueRsPrinter = new PrinterQueueRsPrinter();

                    printerQueueRsPrinter.Id                    = Convert.ToInt32(table.Rows[i][PrinterQueueRsPrinterDesc.Id]);
                    printerQueueRsPrinter.PrinterId             = Convert.ToInt32(table.Rows[i][PrinterQueueRsPrinterDesc.PrinterId]);
                    printerQueueRsPrinter.PrinterQueueId        = Convert.ToInt32(table.Rows[i][PrinterQueueRsPrinterDesc.PrinterQueueId]);
                    printerQueueRsPrinter.Category              = Convert.ToInt32(table.Rows[i][PrinterQueueRsPrinterDesc.Category]);



                    printerQueueRsPrinters.SetValue(printerQueueRsPrinter, i);
                }

                return printerQueueRsPrinters;
            }
            return null;
        }

        public static Int32 Add(PrinterQueueRsPrinter printerQueueRsPrinter)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(PrinterQueueRsPrinterDesc.PrinterId,                printerQueueRsPrinter.PrinterId);
            builder.InsertAdd(PrinterQueueRsPrinterDesc.PrinterQueueId,           printerQueueRsPrinter.PrinterQueueId);
            builder.InsertAdd(PrinterQueueRsPrinterDesc.Category,                 printerQueueRsPrinter.Category);

            builder.QueryTableName = PrinterQueueRsPrinterDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(PrinterQueueRsPrinterDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = PrinterQueueRsPrinterDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(PrinterQueueRsPrinter printerQueueRsPrinter)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(PrinterQueueRsPrinterDesc.PrinterId,                   printerQueueRsPrinter.PrinterId);
            builder.UpdateAdd(PrinterQueueRsPrinterDesc.PrinterQueueId,              printerQueueRsPrinter.PrinterQueueId);
            builder.UpdateAdd(PrinterQueueRsPrinterDesc.Category,                    printerQueueRsPrinter.Category);

            builder.ConditionAdd(PrinterQueueRsPrinterDesc.Id, ConditionExpress.EQUALS, printerQueueRsPrinter.Id);
            builder.QueryTableName = PrinterQueueRsPrinterDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
